var indexVm = new Vue({
	el:'#mui-content',
	data:function() {
		return {
			isLogined: false,
			userName: '',
			role: -1,
			todoItemsHitchData: null,
			todoItemsMaintainData: null,
			todoItemsDemandData: null,
			todoEventItemsDepartData: [],
			ZCCode: '',
			EquipCode: '',
			todoItemLength: 0,
			chartDataState: [],
			chartDataDepart: [],
			chartDataType: [],
			isState: true,
			isDepart: false,
			isType: false
		}
	},
	mounted: function() {
		var self = this;
		mui.init();
		this.initEvent();
		mui.plusReady(function () {
			self.role = plus.storage.getItem('role');
			self.initLayout();
			self.initPage();
		})
	},
	methods:{
		initLayout:function() {
			mui('.mui-scroll-wrapper').scroll({
				scrollY: false, //是否竖向滚动
				scrollX: true, //是否横向滚动
				startX: 0, //初始化时滚动至x
				startY: 0, //初始化时滚动至y
				indicators: true, //是否显示滚动条
				deceleration: 0.0006, //阻尼系数,系数越小滑动越灵敏
				bounce: true //是否启用回弹
			});
			// 图表初始化
			this.equipChart = echarts.init(document.getElementById('equip-chart'));
		},
		initPage: function() {
			this.isLogined = plus.storage.getItem('islogined');
			var loginpage = this.initLoginPage();
			if(this.isLogined == 'true') {
				loginpage.hide();
				this.userName = plus.storage.getItem('name') + "-" + plus.storage.getItem('depart');
				this.initTodoListData();
				this.initDepartEventData()
				this.initChartData();
			} else {
				loginpage.show();
			}
		},
		initChartData: function() {
			var self = this;
			var url = config.requestBaseUrl + "equipmentSumCount";
			var theRole = plus.storage.getItem('role');
			var theDepartid = plus.storage.getItem("departid");
			console.log()
			mui.ajax(url,{
				data:{
					role: theRole,
					departid: theDepartid
				},
				dataType:'json',//服务器返回json格式数据
				type:'get',//HTTP请求类型
				timeout:3000,//超时时间设置为10秒；
				success:function(data){
					if(data.isSuccess) {
						self.chartDataState = data.data.state;
						self.chartDataDepart = data.data.depart;
						self.chartDataType = data.data.type;
						self.drawChart();
					}
				},
				error:function(xhr,type,errorThrown){
					console.log("error")
				}
			});
		},
		changeChart: function(item) {
			switch(item) {
				case 'state':
					this.isState = true;
					this.isDepart = false;
					this.isType = false;
					break;
				case 'depart':
					this.isState = false;
					this.isDepart = true;
					this.isType = false;
					break;
				case 'type':
					this.isState = false;
					this.isDepart = false;
					this.isType = true;
					break;
			}
			this.drawChart();
		},
		drawChart: function() {
			var data = this.chartDataConstructor();
			var option = getPieOption(data);
			this.equipChart.setOption(option);
		},
		chartDataConstructor: function() {
			var result = {
				title: '',
				seriesName: '',
				category: [],
				data: []
			}
			var resourceData = [];
			if(this.isState){
				result.title = "固定资产状态汇总";
				result.seriesName = "状态",
				resourceData = this.chartDataState;
			}
			if(this.isDepart) {
				result.title = "固定资产所属部门汇总";
				result.seriesName = "部门";
				resourceData = this.chartDataDepart;
			}
			if(this.isType) {
				result.title = "固定资产类型汇总";
				result.seriesName = "类型";
				resourceData = this.chartDataType;
			}
			resourceData.forEach(function(item, index) {
				result.category.push(item.text);
				result.data.push({name: item.text, value: item.count});
			})
			return result;
		},
		initEvent: function() {
			var self = this;
			window.addEventListener('logined', function(data) {
				console.log("触发登录事件");
				self.userName = plus.storage.getItem('name') + "-" + plus.storage.getItem('depart');
				self.role = plus.storage.getItem('role');
				self.initTodoListData();
				self.initDepartEventData();
				self.initChartData();
			});
			window.addEventListener('refresh', function(data) {
				console.log("测试")
				self.initTodoListData();
				self.initDepartEventData();
				self.initChartData();
			})
		},
		initTodoListData: function() {
			var self = this;
			console.log("开始请求数据")
			var url = config.requestBaseUrl + "getToDoList";
			var userid = plus.storage.getItem('userid');
			var role = plus.storage.getItem('role');
			
			mui.ajax(url,{
				data:{
					userid: userid,
					role: role
				},
				dataType:'json',//服务器返回json格式数据
				type:'get',//HTTP请求类型
				timeout:3000,//超时时间设置为10秒；
				success:function(data){
					mui.toast(data.msg)
					if(data.isSuccess) {
						self.todoItemsHitchData = data.data.hitch ? data.data.hitch : [];
						self.todoItemsMaintainData = data.data.maintain ? data.data.maintain : [];
						self.todoItemsDemandData = data.data.demand ?  data.data.demand : [];
						self.todoItemLength = self.todoItemsHitchData.length + self.todoItemsMaintainData.length + self.todoItemsDemandData.length;
					}
				},
				error:function(xhr,type,errorThrown){
					console.log(errorThrown);
					mui.toast("出错了!,请联系管理员...")
				}
			});
		},
		initDepartEventData: function() {
			var self = this;
			console.log("开始请求数据")
			var url = config.requestBaseUrl + "getEventByRole";
			var departid = plus.storage.getItem('departid');
			var role = plus.storage.getItem('role');
			
			mui.ajax(url,{
				data:{
					departid: departid,
					role: role
				},
				dataType:'json',//服务器返回json格式数据
				type:'get',//HTTP请求类型
				timeout:3000,//超时时间设置为10秒；
				success:function(data){
					console.log(data.msg)
					if(data.isSuccess) {
						self.todoEventItemsDepartData = data.data ? data.data : [];
					}
				},
				error:function(xhr,type,errorThrown){
					console.log(errorThrown);
					mui.toast("出错了!,请联系管理员...")
				}
			});
		},
		initLoginPage: function() {
			var login = plus.webview.getWebviewById('login');
			if(!login) {
				login = plus.webview.create('pages/login.html', 'login', {
					left: '0%'
				})
			}
			return login;
		},
		openBarcode: function () {
			
			//设置状态栏的颜色
			// plus.navigator.setStatusBarStyle("UIStatusBarStyleBlackOpaque");
			// plus.navigator.setStatusBarBackground('#000000');
			createWithoutTitle('./pages/barcode_scan.html', {
				titleNView:{
					type: 'float',
					backgroundColor: 'rgba(215,75,40,0.3)',
					titleText: '扫一扫',
					titleColor: '#FFFFFF',
					autoBackButton: true,
					buttons: [{
						fontSrc: '_www/helloh5.ttf',
						text: '\ue302',
						fontSize: '18px',
						onclick: 'javascript:scanPicture()'
					}]
				}
			});
		},
		scaned:function(t, r, f) {
			if(t == 'CODE93' || t == 'RQ') {
				this.ZCCode = r;
			} else if(t == 'CODE128'){
				this.TMCode = r;
			}
			this.isEquipRecorded(r, t)
		},
		isEquipRecorded: function(code, type) {
			var self = this;
			var url = config.requestBaseUrl + 'isEquipRecorded';
			mui.ajax(url,{
				data:{
					zcbh: this.ZCCode,
					tmbh: this.TMCode
				},
				dataType:'json',//服务器返回json格式数据
				type:'post',//HTTP请求类型
				timeout:10000,//超时时间设置为10秒；
				success:function(data){
					if(data.isSuccess) {
						console.log("isrecorded:" + data.isRecorded);
						if(!data.isRecorded) {
							// 制衣办编号, 需要后端返回,不可修改
							var zybbh = data.data;
							self.toAddNewEquipPage(code, zybbh, type);
						} else {
							self.toEquipManagePage(code, type);
						}
					} else {
						mui.toast("出错啦!请联系管理员...");
					}
				},
				error:function(xhr,type,errorThrown){
					mui.toast("出错啦!请联系管理员...");
				}
			});
		},
		toAddNewEquipPage: function(code, zybbh, type) {
			var addNewPage = plus.webview.getWebviewById('addNewEquip');
			if(!addNewPage) {
				addNewPage = plus.webview.create(
				'pages/addEquipment.html', 
				'addNewEquip', 
				{left: '0%'},
				{
					code: code,
					zybbh: zybbh,
					type: type
				});
			}
			addNewPage.show(
			{extras: {
					code: code,
					zybbh: zybbh,
					type: type
				},
			});
		},
		toEquipManagePage: function(code, type) {
			/**
			 * 去资产管理界面
			 */
			console.log("code:" + code);
			console.log("type:" + type);
			var equipManagePage = plus.webview.getWebviewById('equipManage');
			if(!equipManagePage) {
				equipManagePage = plus.webview.create(
				'pages/equipmentManage.html', 
				'equipManage', 
				{left: '0%'},
				{
					code: code,
					type: type
				});
			}
			equipManagePage.show(
			{extras: {
					code: code,
					type: type
				},
			});
		},
		recordEvent: function() {
			/**
			 * 跳转至事件记录页面
			 */
			var recordEventPage = plus.webview.getWebviewById('recordEvent');
			if(!recordEventPage) {
				recordEventPage = plus.webview.create(
				'pages/recordEvent.html', 
				'recordEvent', 
				{left: '0%'});
			}
			recordEventPage.show();
		},
		recordDemand: function() {
			/**
			 * 跳转至事件记录页面
			 */
			var recordDemandPage = plus.webview.getWebviewById('recordDemand');
			if(!recordDemandPage) {
				recordDemandPage = plus.webview.create(
				'pages/recordDemand.html', 
				'recordDemand', 
				{left: '0%'});
			}
			recordDemandPage.show();
		},
		queryData: function() {
			/**
			 * 跳转至数据查询界面
			 */
			var queryDataPage = plus.webview.getWebviewById('queryData');
			if(!queryDataPage) {
				queryDataPage = plus.webview.create(
				'pages/queryData.html', 
				'queryData', 
				{left: '0%'});
			}
			queryDataPage.show();
		},
		toEvent: function(item) {
			var eventPage = plus.webview.create('pages/eventHandle.html', 'eventHandle',{
				left: '0%'
			},item);
			eventPage.show();
		},
		toMaintain: function(item){
			console.log("id:" + item.id)
			var maintainPage = plus.webview.create('pages/maintain.html', 'maintain',{
				left: '0%'
			}, {
				equipmentid: item.equipmentid,
				maintainid: item.id,
				isInMaintain: true
			});
			maintainPage.show();
		},
		toDemand: function(item) {
			var demandPage = plus.webview.create('pages/demandHandle.html', 'demandHandle',{
				left: '0%'
			}, {
				data: item
			});
			demandPage.show();
		},
		logout: function() {
			var btnArray = [{title:"退出登录",style:"destructive"}];
			plus.nativeUI.actionSheet( {
				buttons:btnArray
			}, function(e){
				if(e.index == 1) {
					plus.storage.setItem('islogined', 'false');
					plus.storage.setItem('account', '');
					plus.storage.setItem('type','');
					plus.storage.setItem('name','');
					plus.storage.setItem('role', '');
					plus.storage.setItem('depart', '');
					
					window.removeEventListener('logined', function() {});
					window.removeEventListener('refresh',function() {});
					plus.runtime.quit();
				}
			});
		}
	}
})

function scaned(t, r, f){
	indexVm.scaned(t, r, f);
}